package com.hspedu.homework;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/12
 * @time 16:46
 */
public class Homework02 {
    public static void main(String[] args) {


    }


    @Test
    public void test(){
        DAO<User> userDAO = new DAO<>();
        userDAO.save("1",new User(1,18,"jack"));
        userDAO.save("2",new User(2,18,"lucy"));
        userDAO.get("1");
        userDAO.update("1",new User(1,20,"jack"));
        System.out.println(userDAO.list());

        userDAO.delete("1");
        System.out.println(userDAO.list());
    }


}

class DAO<T>{
    private Map<String,T> map = new HashMap<>();

    public void save(String id , T entity){
        map.put(id,entity);
    }

    public T get(String id){
        return map.get(id);
    }

    public void update(String id , T entity){
        map.put(id,entity);
    }

    public List<T> list(){
        List<T> list = new ArrayList<>();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            list.add(map.get(key));
        }
        return list;
    }

    public void delete(String id){
          map.remove(id);
    }

}


class User{
    private int id;
    private int age;
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
}
